package miner;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import tool.MUser;
import weibo4j.model.User;

import lib.Db;
import lib.TableBasicInfo;
import localreader.UserMa2;

public class GetUserBasicInfo {
	/**
	 * @param args
	 * @throws IOException 
	 * @throws InterruptedException 
	 * @throws SQLException 
	 */
	public static void main(String[] args) throws IOException, InterruptedException, SQLException {
		getSingleUser("2000000002");
	}
	
/*	public static void getSet1() throws IOException, InterruptedException, SQLException{
		UserMa userMa = new UserMa();
		List<String> users = userMa.getN1();
		users.addAll(userMa.getA1());
		int size = users.size();
		for(int i = 0; i < size; i++){
			User u = MUser.GetUserByUid(users.get(i));
			if(u == null){
				System.out.println(users.get(i) + "error occur, try again");
				i--;
			}else{
				Connection con = Database.connectSQL();
				if(Database.exitUser(u, con) != true)
					Database.insertUser(u, con);
			}
		}
	}
	
	public static void getSet2() throws IOException, InterruptedException, SQLException{
		UserMa2 userMa2 = new UserMa2();
		List<String> users = userMa2.getNo();
		users.addAll(userMa2.getAb());
		users.addAll(userMa2.getNu());
		users.addAll(userMa2.getAd());
		int size = users.size();
		for(int i = 0; i < size; i++){
			User u = MUser.GetUserByUid(users.get(i));
			if(u == null){
				System.out.println(users.get(i) + "error occur, try again");
				i--;
			}else{
				Connection con = Database.connectSQL();
				if(Database.exitUser(u, con) != true)
					Database.insertUser(u, con);
			}
		}
	}
*/
	public static void getSet2No(String begin){
		UserMa2 userMa2 = new UserMa2();
		List<String> users = userMa2.getNoUsers();
		System.out.println("user number: "+users.size());
	
		Connection con = Db.connect();
		if(con == null ){
			System.out.println("Connect to database failed");
			return;
		}
		
		Iterator<String> index =users.iterator();
		
		if(begin != null){
			while(index.hasNext()){
				if(begin.equals(index.next())){
					break;
				}
			}
		}
		
		while(index.hasNext()){
			String uid = index.next();
			User u = MUser.GetUserByUid(uid);
			if(u == null){
				System.out.println(uid+": get basic info failed.");
			}else{
				if(TableBasicInfo.existUser(u, con) != true){
					TableBasicInfo.insertUser(u, con);
				}
				System.out.println(uid+": insert basic info finished");
			}
		}
		
		Db.disconnect();
	}
	
	public static void getSet2Ad(String begin){
		UserMa2 userMa2 = new UserMa2();
		List<String> users = userMa2.getAdUsers();
		System.out.println("user number: "+users.size());
	
		Connection con = Db.connect();
		if(con == null ){
			System.out.println("Connect to database failed");
			return;
		}
		
		for(int i = 0; i < users.size(); i++){
			User u = MUser.GetUserByUid(users.get(i));
			if(u == null){
				System.out.println(users.get(i)+": get basic info failed.");
			}else{
				if(TableBasicInfo.existUser(u, con) != true){
					TableBasicInfo.insertUser(u, con);
				}
				System.out.println(users.get(i)+": insert basic info finished");
			}
		}
		
		Db.disconnect();
	}
	
	public static void getSingleUser(String uid){
		Connection con = Db.connect();
		
		User u = MUser.GetUserByUid(uid);
		if(u == null){
			System.out.println(uid+": get basic info failed.");
		}else{
			if(TableBasicInfo.existUser(u, con) != true){
				TableBasicInfo.insertUser(u, con);
			}
			System.out.println(uid+": insert basic info finished");
		}
		
		Db.disconnect();
		
	}
	
	private static List<String> getIds(String set){
		
	}
/*	
	public static void updateSet1() throws IOException, SQLException, InterruptedException{
		UserMa userMa = new UserMa();
		List<String> users = userMa.getN1();
		users.addAll(userMa.getA1());
		int size = users.size();
		Connection con = Database.connectSQL();
		for(int i = 0; i < size; i++){	
			if(Database.exitUser(users.get(i), con)!=true){
				User u = MUser.GetUserByUid(users.get(i));
				if(u == null){
					System.out.println(users.get(i) + "error occur, try again");
					i--;
				}
				else{
					Database.insertUser(u, con);
				}
			}else
				continue;
		}	
	}
	
	
	public static void getSingle(String uid) throws InterruptedException, SQLException{
		User u = MUser.GetUserByUid(uid);
		int flagError = 0;
		do{
			if(u == null){
				flagError++;
				if(flagError == 5)
					System.out.println(uid + " get basicinfo failed.");
				else 
					System.out.println(uid + "error occur, try again");
			}else{
				Connection con = Database.connectSQL();
				if(Database.exitUser(u, con) != true)
					Database.insertUser(u, con);
				break;
			}
		}while(flagError < 5);
		
	}
*/
}
